package com.zsj.hello.first;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author https://gitee.com/zhengshengjun
 * @date 2023/8/29.
 */
public class Consumer {
    public static final String QUEUE_HELLO = "hello";


    public static void main(String[] args) throws IOException, TimeoutException {
        //链接rabbitmq 队列
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.47.128");
        factory.setUsername("admin");
        factory.setPassword("123456");

        //建立连接
        Connection connection = factory.newConnection();

        //信道
        Channel channel = connection.createChannel();


        //声明 接收小心
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //message就是发送的消息 因为这里使用的是一个字符串 所以直接打印
            System.out.println(consumerTag + "consumer tag");
            System.out.println(new String(message.getBody()));
        };
        //声明取消后的回调
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消费消息被中断  取消消费");
        };


        /**
         * 消费者接收消息
         * 1、消费哪个队列
         * 2、消费成功后是否自动应答
         * 3、消费失败后的回调 函数型接口
         * 4、取消之后的回调 函数型接口
         */
        channel.basicConsume(QUEUE_HELLO, true, deliverCallback, cancelCallback);

    }
}
